package com.alibaba.dingtalk.openapi.helper;

import com.alibaba.dingtalk.openapi.exception.OApiException;
import com.alibaba.dingtalk.openapi.exception.OApiResultException;
import com.alibaba.dingtalk.openapi.utils.DingtalkSignHelper;
import com.alibaba.dingtalk.openapi.utils.HttpHelper;
import com.alibaba.dingtalk.openapi.utils.aes.Utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.BundleErrorCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

/**
 * @author chenshan
 */
@Component
public class MiniMemberInfoHelper {
    
    private static final Logger LOG = LoggerFactory.getLogger(MiniMemberInfoHelper.class);
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private AuthHelper authHelper;
    
    public String createGroup(String corpid, String title, String ownerUserId, String userIds) throws XbbException,OApiException {
        String accessToken = authHelper.getAccessToken(corpid);
        String url = Env.OAPI_HOST + "/topapi/im/chat/scenegroup/create?access_token=" + accessToken;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("icon", "");
        jsonObject.put("management_type", 1L);
        jsonObject.put("owner_user_id", ownerUserId);
        jsonObject.put("searchable", 1L);
        jsonObject.put("show_history_type", 1L);
        jsonObject.put("template_id", proBaseConfig.getGroupTemplateId());
        jsonObject.put("title", title);
        jsonObject.put("user_ids", userIds);
        jsonObject.put("validation_type", 0L);
        JSONObject response = HttpHelper.httpPostDingtalk(url, jsonObject);
		Boolean success = response.getBoolean("success");
		if ( Objects.isNull(success) || !success ) {
			throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010019);
		}
		JSONObject result = response.getJSONObject("result");
		if ( Objects.isNull(result) ) {
			throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010019);
		}
        return result.getString("open_conversation_id");
    }
    
    public void addMember(String openConversationId, String userIds,String corpid) throws XbbException,OApiException {
        String accessToken = authHelper.getAccessToken(corpid);
        String url = Env.OAPI_HOST + "/topapi/im/chat/scenegroup/member/add?access_token=" + accessToken;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("open_conversation_id", openConversationId);
        jsonObject.put("user_ids", userIds);
        JSONObject response = HttpHelper.httpPostDingtalk(url, jsonObject);
        Boolean success = response.getBoolean("success");
        if ( Objects.isNull(success) || !success ) {
            throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010013);
        }
    }
    
    public void deleteMember(String openConversationId, String userIds,String corpid) throws XbbException,OApiException {
        String accessToken = authHelper.getAccessToken(corpid);
        String url = Env.OAPI_HOST + "/topapi/im/chat/scenegroup/member/delete?access_token=" + accessToken;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("open_conversation_id", openConversationId);
        jsonObject.put("user_ids", userIds);
        JSONObject response = HttpHelper.httpPostDingtalk(url, jsonObject);
        Boolean success = response.getBoolean("success");
        if ( Objects.isNull(success) || !success ) {
            throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010012);
        }
    }
    
    public JSONObject getGroupInfo(String accessToken,String openConversationId) throws XbbException, OApiException {
        String url = Env.OAPI_HOST + "/topapi/im/chat/scenegroup/get?access_token=" + accessToken;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("open_conversation_id", openConversationId);
        JSONObject response = HttpHelper.httpPostDingtalk(url, jsonObject);
        Boolean success = response.getBoolean("success");
        if ( Objects.isNull(success) || !success ) {
            throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010017);
        }
        JSONObject result = response.getJSONObject("result");
        if ( Objects.isNull(result) ) {
            throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010017);
        }
        return result;
    }
    
    public JSONObject getGroupMemberInfo(String accessToken,String openConversationId,String cursor) throws XbbException,OApiException{
        String url = Env.OAPI_HOST + "/topapi/im/chat/scenegroup/member/get?access_token=" + accessToken;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("open_conversation_id", openConversationId);
        jsonObject.put("cursor", cursor);
        jsonObject.put("size", 100L);
        JSONObject response = HttpHelper.httpPostDingtalk(url, jsonObject);
        Boolean success = response.getBoolean("success");
        if ( Objects.isNull(success) || !success ) {
            throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010018);
        }
        JSONObject result = response.getJSONObject("result");
        if ( Objects.isNull(result) ) {
            throw new XbbException(BundleErrorCodeEnum.API_ERROR_1010018);
        }
        return result;
    }
}
